home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 19 / CU Amiga Magazine's Super CD-ROM 19 (1998)(EMAP Images)(GB)[!][issue 1998-02].iso / CUCD / Online / NNTPd / server / scandir.c < prev    next >
Encoding:
C/C++ Source or Header  |  2002-11-05  |  1.9 KB  |  100 lines

  1. #ifndef lint
  2. static char    sccsid[] = "@(#)$Id: scandir.c,v 1.9 1994/11/05 06:04:10 sob Exp sob $";
  3. #endif
  4.  
  5. #include "common.h"
  6.  
  7. /*
  8.  * scan_dir -- scan the current directory for news articles,
  9.  *    loading the article numbers into art_array.  Return
  10.  *    number of articles loaded.
  11.  *
  12.  *    Parameters:    "low_msg", "high_msg" are the low
  13.  *            and high messages numbers in this
  14.  *            group; we ignore numbers outside this
  15.  *            range.
  16.  *
  17.  *    Returns:    Number of articles loaded into
  18.  *            array.
  19.  *
  20.  *    Side effects:    Changes "art_array".
  21.  */
  22.  
  23. extern    int    intcmp();
  24. extern char *malloc(), *realloc();
  25.  
  26. int
  27. scan_dir(low_msg, high_msg)
  28. int    low_msg, high_msg;
  29. {
  30. #ifdef DIRENT
  31.     register struct dirent    *dirent;
  32. #else
  33.     register struct direct    *dirent;
  34. #endif
  35.     register DIR        *dirp;
  36.     int            artnum;
  37.  
  38.     num_arts = 0;
  39.  
  40.     dirp = opendir(".");
  41.  
  42.     if (dirp == NULL)
  43.         return (0);
  44.  
  45.     while ((dirent = readdir(dirp)) != NULL) {
  46.         artnum = atoi(dirent->d_name);
  47.         if (artnum == 0 || artnum < low_msg || artnum > high_msg)
  48.             continue;
  49. #ifdef DYNAMIC_ART_ARRAY
  50.         /* Expand/allocate art_array elements as necessary */
  51.         if (num_arts >= size_art_array) {
  52.             size_art_array += 1024;
  53.             if (art_array)
  54.                 art_array = (int *)realloc(art_array,
  55.                     size_art_array * sizeof *art_array);
  56.             else
  57.                 art_array = (int *)
  58.                     malloc(size_art_array * sizeof *art_array);
  59.             if (art_array == 0) {
  60. #ifdef SYSLOG
  61.                 syslog(LOG_ERR,
  62.                     "scan_dir(): malloc/realloc failed");
  63. #endif
  64.                 num_arts = 0;
  65.                 size_art_array = 0;
  66.                 closedir(dirp);
  67.                 return(0);
  68.             }
  69.         }
  70. #endif
  71.         art_array[num_arts++] = artnum;
  72.  
  73.     }
  74.     closedir(dirp);
  75.  
  76.     qsort((char *) art_array, num_arts, sizeof(int), intcmp);
  77.  
  78.     return (num_arts);
  79. }
  80.  
  81.  
  82. /*
  83.  * intcmp -- compare to integers.
  84.  *
  85.  *    Parameters:    "x", "y" point to the integers to be compared.
  86.  *
  87.  *    Returns:    -1 if "x" is less than "y",
  88.  *            0 if "x" equals "y", and
  89.  *            1 if "x" is greater than "y".
  90.  *
  91.  *    Side effects:    None.
  92.  */
  93.  
  94. int
  95. intcmp(x, y)
  96. register int    *x, *y;
  97. {
  98.     return (*x - *y);
  99. }
  100.